/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.onsite;

import cn.ac.nercis.pes.community.controller.evaluation.onsite.assets.AssetManageEvalController;
import cn.ac.nercis.pes.community.controller.evaluation.onsite.assets.AssetTechnologyEvalController;
import cn.ac.nercis.pes.community.model.vo.MenuVO;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.utils.ControlUtils;
import cn.ac.nercis.pes.community.utils.LogUtils;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import cn.ac.nercis.pes.community.views.evaluation.onsite.assets.AssetManageEvalView;
import cn.ac.nercis.pes.community.views.evaluation.onsite.assets.AssetTechnologyEvalView;
import cn.ac.nercis.pes.model.dto.knowledge.KmSimpleTargetDTO;
import cn.ac.nercis.pes.service.evaluation.EvaluationRangeService;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

/**
 * 现场测评资产对象内容管理控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class AssetContentController implements Initializable {
    @FXML
    private StackPane layerPane;
    @FXML
    private AnchorPane emptyPane;
    @FXML
    private ImageView emptyImageView;
    @FXML
    private Label emptyLabel;

    private final EvaluationRangeService evaluationRangeService;

    private MenuVO groupAsset;
    private MenuVO currentAsset;
    private String currentSystemId;
    private KmSimpleTargetDTO currentLayerTarget;

    public AssetContentController() {
        this.evaluationRangeService = SpringUtils.getBean(EvaluationRangeService.class);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ControlUtils.initEmptyImageView(this.emptyImageView,log);
    }

    public void initializeData(String systemId,MenuVO currentAsset){
        initializeData(systemId, currentAsset, null);
    }

    public void initializeData(String systemId,MenuVO currentAsset,MenuVO groupAsset){
        LogUtils.info(log, "初始化现场测评资产对象内容...");
        this.currentSystemId = systemId;
        this.currentAsset = currentAsset;
        this.groupAsset = groupAsset;
        this.loadLayerTabData();
    }

    private void loadLayerTabData() {
        LogUtils.info(log, "获取系统资产关联层面指标信息...");
        LogUtils.record(log, "请求数据，systemId：{},assetId:{}", this.currentSystemId, currentAsset.getCode());
        CompletableFuture.supplyAsync(() -> evaluationRangeService.findLayerTargetByAsset(this.currentSystemId, currentAsset.getCode()))
                .thenAccept(res -> Platform.runLater(() -> {
                    if (res.isSuccess()) {
                        this.layerPane.setVisible(true);
                        this.emptyPane.setVisible(false);
                        res.getData().stream().findFirst().ifPresent(this::initLayerData);
                    } else {
                        this.layerPane.setVisible(false);
                        this.emptyPane.setVisible(true);
                        this.emptyLabel.setText(res.getMsg());
                    }
                }))
                .exceptionally((ex) -> {
                    log.error("获取系统资产关联层面指标信息出错!", ex);
                    return null;
                });
    }

    private void initLayerData(KmSimpleTargetDTO layerTarget){
        this.currentLayerTarget = layerTarget;
        this.layerPane.getChildren().clear();
        switch (layerTarget.getType()) {
            case TECHNOLOGY -> {
                var view = AppStore.getEvaluationView(this.currentSystemId, AssetTechnologyEvalView.class);
                this.layerPane.getChildren().add(Objects.requireNonNull(view).load());
                if (view.getController() instanceof AssetTechnologyEvalController controller) {
                    controller.initializeData(this.currentSystemId, currentAsset, groupAsset, layerTarget);
                }
            }
            case MANAGEMENT -> {
                var view = AppStore.getEvaluationView(this.currentSystemId, AssetManageEvalView.class);
                this.layerPane.getChildren().add(Objects.requireNonNull(view).load());
                if (view.getController() instanceof AssetManageEvalController controller) {
                    controller.initializeData(this.currentSystemId, currentAsset, layerTarget);
                }
            }
            default -> {
                this.layerPane.setVisible(false);
                this.emptyPane.setVisible(true);
                this.emptyLabel.setText("不支持的层面指标测评");
            }
        }
    }

}